package com.hq.ims.data.event.listener;

import com.hq.ims.data.entity.BusInfoNotification;
import com.hq.ims.data.entity.BusInfoTrack;
import com.hq.ims.data.entity.CompanyEmployee;
import com.hq.ims.data.entityenum.EnumBusKey;
import com.hq.ims.data.entityenum.EnumUserTodoType;
import com.hq.ims.data.event.BusInfoForewarningEvent;
import com.hq.ims.data.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

/**
 * 用户跟踪反馈事件
 *
 * @date 2022/6/7 15:41
 */
@Component
@Slf4j
public class BusInfoForewarningEventListener{

    @Resource
    private BaseCompanyService baseCompanyService;
    @Resource
    private BaseCompanyEmployeeService baseCompanyEmployeeService;
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private BasePlatformTodoStatisticsService basePlatformTodoStatisticsService;
    @Resource
    private BaseUserTodoStatisticsService baseUserTodoStatisticsService;
    @Resource
    private BaseBusiInfoService baseBusiInfoService;

    @Async
    @TransactionalEventListener
    public void onApplicationEvent(BusInfoForewarningEvent businfoEvent) {
        EnumBusKey forewarningType = businfoEvent.getForewarningType();
        switch (forewarningType) {
            case PairingNotAgree:
                this.pairingNotAgreeHandler(businfoEvent);
                break;
            case ProjectNotDiscussed:
            case ProjectFollowUp:
            case ProjectAlreadyDeliverTender:
            case ProjectWinTheBid:
            case ProjectSignAContract:
            case Other:
                this.busTrackHandler(businfoEvent);
                break;
            default:
                break;
        }


    }

    /**
     * 未接收处理
     */
    public void pairingNotAgreeHandler(BusInfoForewarningEvent businfoEvent) {
        //用户端经营信息预警 + 1,这里应该是给管理员 + 1
        BusInfoNotification busInfoNotification = businfoEvent.getBusInfoNotification();
        CompanyEmployee companyEmployee = baseCompanyEmployeeService.getCompanyAdminByCompanyId(busInfoNotification.getCompanyId());
        baseUserTodoStatisticsService.updateCountByUserTodoType(
                EnumUserTodoType.BusinfoForewarningNotifyCount,
                1,
                businfoEvent.getBusInfoNotification().getBusiInfoId(),
                companyEmployee.getUserId(),
                busInfoNotification.getCompanyId()
        );
    }

    /**
     * 追踪处理
     */
    public void busTrackHandler(BusInfoForewarningEvent businfoEvent) {
        BusInfoTrack busInfoTrack = businfoEvent.getBusInfoTrack();
        //用户端经营信息预警 + 1
        baseUserTodoStatisticsService.updateCountByUserTodoType(
                EnumUserTodoType.BusinfoForewarningNotifyCount,
                1,
                businfoEvent.getBusInfoTrack().getBusInfoTrackId(),
                busInfoTrack.getBusiPersonId(),
                busInfoTrack.getCompanyId()
        );
    }
}
